package com.exchange.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exchange.entity.pojo.CardDisplay;
import com.exchange.entity.vo.CouponVo;
import com.exchange.mapper.CardDisplayMapper;
import com.exchange.service.CardDisplayService;
import com.exchange.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author Administrator
* @description 针对表【card_display】的数据库操作Service实现
* @createDate 2024-04-25 20:02:10
*/
@Service
public class CardDisplayServiceImpl extends ServiceImpl<CardDisplayMapper, CardDisplay>
    implements CardDisplayService{
    @Autowired
    private CardDisplayMapper cardDisplayMapper;
    @Override
    public Result addCoupon(CouponVo couponVo) {
        if (ObjectUtil.isEmpty(couponVo)){
            return Result.error("参数不能为空");
        }
        Integer type = couponVo.getType();
        if (type==0){
            CardDisplay cardDisplay = new CardDisplay();
            cardDisplay.setCardName(couponVo.getCardName());
            cardDisplay.setCountPrice(BigDecimal.valueOf(couponVo.getDay()));
            cardDisplay.setType(couponVo.getType());
            cardDisplay.setDurableTime(couponVo.getDurableTime());
            this.cardDisplayMapper.insert(cardDisplay);
        }else {
            CardDisplay cardDisplay = new CardDisplay();
            cardDisplay.setCardName(couponVo.getCardName());
            cardDisplay.setDay(couponVo.getDay());
            cardDisplay.setType(couponVo.getType());
            cardDisplay.setDurableTime(couponVo.getDurableTime());
            this.cardDisplayMapper.insert(cardDisplay);
        }
        return Result.success("添加成功");
    }

    @Override
    public Page<CardDisplay> pages(Integer offset, Integer pagesize,Integer type) {
        IPage<CardDisplay> page = new Page<>(offset,pagesize);
//        page.orders().add(OrderItem.desc("create_time"));
//        QueryWrapper<CardDisplay> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("type",type).orderByDesc("create_time");
        IPage<CardDisplay> page1 = this.cardDisplayMapper.select(page, type);
        return (Page<CardDisplay>) page1;
    }
}




